package com.housediyadmin.service
{
	import com.housediyadmin.events.roomEvents.GetRoomEvent;
	import com.housediyadmin.vo.Room;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/12/2
	 */	
	public class RoomRemoteObjectService extends Actor
	{
		public var roomRemoteObject:RemoteObject;
		
		public function RoomRemoteObjectService()
		{
			super();
			
			var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediyadmin/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.1.120/HouseDiyAdmin/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			roomRemoteObject = new RemoteObject("fluorine");
			roomRemoteObject.source = "HouseDiyService.HouseDiyAction";
			roomRemoteObject.showBusyCursor = true;
			roomRemoteObject.channelSet = channelSet;
			
			/*roomRemoteObject.getOperation("getAllRoom").addEventListener(ResultEvent.RESULT, getAllRoomsResult);
			roomRemoteObject.getOperation("getAllRoom").addEventListener(FaultEvent.FAULT, faultHandler);
			
			roomRemoteObject.getOperation("getRecordsRoom").addEventListener(ResultEvent.RESULT, getRecordsRoomResult);
			roomRemoteObject.getOperation("getRecordsRoom").addEventListener(FaultEvent.FAULT, faultHandler);
			
			roomRemoteObject.getOperation("addRoom").addEventListener(ResultEvent.RESULT, addRoomResult);
			roomRemoteObject.getOperation("addRoom").addEventListener(FaultEvent.FAULT, faultHandler);
		
			roomRemoteObject.getOperation("editRoom").addEventListener(ResultEvent.RESULT, editRoomResult);
			roomRemoteObject.getOperation("editRoom").addEventListener(FaultEvent.FAULT, faultHandler);
		
			roomRemoteObject.getOperation("deleteRooms").addEventListener(ResultEvent.RESULT, deleteRoomsResult);
			roomRemoteObject.getOperation("deleteRooms").addEventListener(FaultEvent.FAULT, faultHandler);*/
		}
		
		
		public function getAllRooms():void
		{
			roomRemoteObject.getAllRoom();
		}
		
		/**
		 *参数为一串房型id，得到相应的房型信息，
		 * @param roomIds:ArraryCollection
		 * 
		 */		
		public function getRoomByIds(roomIds:ArrayCollection):void
		{
			roomRemoteObject.getRecordsRoom(roomIds);
		}
		
		/**
		 * 参数为一个room类型的值，
		 * @param room:Room
		 * 
		 */		
		public function addOneRoom(room:Room):void
		{
			roomRemoteObject.addRoom(room);
		}
		
		/**
		 * 此处传入的Room类型的参数，需要roomId
		 * @param room:Room
		 * 
		 */		
		public function editOneRoom(room:Room):void
		{
			roomRemoteObject.editRoom(room);
		}
		
		/**
		 *根据传入的room的Id删除相应的记录，可以为一条或多条 记录
		 * @param roomIds
		 * 
		 */				
		public function deleteRoomByIds(roomIds:Array):void
		{
			roomRemoteObject.deleteRooms(roomIds);
		}
		
		private function getAllRoomsResult(event:ResultEvent):void
		{
			dispatch(new GetRoomEvent(GetRoomEvent.UPDATE_ROOM_MODEL, event.result as ArrayCollection));
		}
		
		private function getRecordsRoomResult(event:ResultEvent):void
		{
			
		}
		
		private function addRoomResult(event:ResultEvent):void
		{
			dispatch(new GetRoomEvent(GetRoomEvent.UPDATE_ROOM_MODEL, event.result as ArrayCollection));
		}
		
		private function editRoomResult(event:ResultEvent):void
		{
			dispatch(new GetRoomEvent(GetRoomEvent.UPDATE_ROOM_MODEL, event.result as ArrayCollection));
		}
		
		private function deleteRoomsResult(event:ResultEvent):void
		{
			dispatch(new GetRoomEvent(GetRoomEvent.UPDATE_ROOM_MODEL, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}